﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebMaster_UploadAdvImage : System.Web.UI.Page
{
    private readonly string m_FolderImage = GlobalClassBE.FolderPhotoData + @"Advertise\";

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadPage();
    }

    #region load page

    private void LoadPage()
    {
        string adv = "";
        if (Request.Params["adv"] != null)
            adv = Request.Params["adv"].ToString();

        GssDO.DOAdvertise advDO = getAdvertiseByID(adv);
        if (advDO != null && advDO.IsDeleted == 0)
        {
            string func = "";
            if (Request.Params[GlobalClassBE.MyUrlParameter.ActionName] != null)
                func = Request.Params[GlobalClassBE.MyUrlParameter.ActionName].ToString();

            pnlList.Visible = false;
            pnlAddEdit.Visible = false;
            pnlDelete.Visible = false;

            GlobalClassBE.MyMessage.MgsCurrentTitle = GlobalClassBE.PageName.AdvertiseTitle;

            if (func == "" || func == "list")
            {
                pnlList.Visible = true;
                LoadList(adv);
            }
            else if (func == "add" || func == "edit")
            {
                pnlAddEdit.Visible = true;
                LoadInput(func);
            }
            else if (func == "delete")
            {
                pnlDelete.Visible = true;
                LoadDelete();
            }
        }
        else
            Response.Redirect(@"Advertise.aspx", false);
    }

    private void LoadList(string id)
    {
        DataTable dtData = GetDataSource(id);
        string[] astr = new string[1];
        GssDO.DOAdvImage DO = new GssDO.DOAdvImage();
        astr[0] = DO.PKIDAdvImageColumnName;
        GlobalClassBE.SetGridCss(this.grdData);
        this.ShowTextForList();
        this.grdData.AutoGenerateColumns = false;
        this.grdData.AllowPaging = true;
        this.grdData.DataKeyNames = astr;
        this.grdData.DataSource = dtData;
        this.grdData.DataBind();

        if (dtData == null || dtData.Rows.Count == 0)
            pnlData.Visible = false;
        else
            pnlData.Visible = true;
    }

    private void LoadInput(string actName)
    {
        ShowTextForInput();
        ckbDeleteImages.Attributes.Add("onClick", "checkUpload(this,'" + fulImages.ClientID + "')");
        if (!Page.IsPostBack)
        {
            string id = "";
            if (Request.Params["id"] != null)
                id = Request.Params["id"].ToString();

            GlobalClassBE.MyMessage.MgsCurrentMessages = "";

            if (actName == "add")
                ShowToAdd();
            else if (actName == "edit")
            {
                if (id != "")
                    ShowToEdit(id);
                else
                {
                    GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Selected;
                    CancelPage();
                }
            }
            else
            {
                CancelPage();
            }
        }
    }

    private void LoadDelete()
    {
        string id = "";
        if (Request.Params["id"] != null)
            id = Request.Params["id"].ToString();

        GlobalClassBE.MyMessage.MgsCurrentMessages = "";

        if (id != "")
            ShowToDelete(id);
        else
            CancelPage();
    }

    #endregion load page


    #region show text

    private void ShowTextForList()
    {
        GssDO.DOAdvImage DO = new GssDO.DOAdvImage();
        Hashtable htb = new Hashtable();
        htb.Add(DO.TitleColumnName, GlobalClassBE.GridText.Title);
        htb.Add(DO.URLColumnName, GlobalClassBE.GridText.URL);
        htb.Add(DO.ImageColumnName, GlobalClassBE.GridText.Image);
        lkbAddNew.Text = GlobalClassBE.ActionText.Add;
        for (int i = 0; i < this.grdData.Columns.Count; i++)
        {
            if (this.grdData.Columns[i].GetType().Name == "CommandField")
            {
                if (((CommandField)this.grdData.Columns[i]).ShowEditButton)
                {
                    ((CommandField)this.grdData.Columns[i]).EditText = GlobalClassBE.ActionText.Edit;
                }
                if (((CommandField)this.grdData.Columns[i]).ShowDeleteButton)
                {
                    ((CommandField)this.grdData.Columns[i]).DeleteText = GlobalClassBE.ActionText.Delete;
                }
            }
            else if (this.grdData.Columns[i].Visible)
            {
                string s = ((BoundField)this.grdData.Columns[i]).DataField;
                ((BoundField)this.grdData.Columns[i]).HeaderText = htb[s].ToString();
            }
        }
    }

    private void ShowTextForInput()
    {
        lblTitle.Text = GlobalClassBE.LabelText.Title;
        lblImage.Text = GlobalClassBE.LabelText.Filename;
        lblURL.Text = "URL:";
        lblWidth.Text = "Width:";
        lblHeight.Text = "Height:";
        lblLevel.Text = GlobalClassBE.LabelText.Level;

        reqFormatImage.Text = GlobalClassBE.MyMessage.MgsFileType;
        reqFormatNumberWidth.ErrorMessage = GlobalClassBE.MyMessage.MgsNumberFormatFrom0To9999;
        reqFormatNumberHeight.ErrorMessage = GlobalClassBE.MyMessage.MgsNumberFormatFrom0To9999;
        reqFormatNumberLevel.ErrorMessage = GlobalClassBE.MyMessage.MgsNumberFormatFrom0To9999;
        reqFormatImage.Text = GlobalClassBE.MyMessage.MgsFileType;
        reqField.Text = GlobalClassBE.MyMessage.MgsRequiredField;

        btnSave.Text = GlobalClassBE.ButtonText.BtnSave;
        btnClear.Text = GlobalClassBE.ButtonText.BtnReset;
        btnCancel.Text = GlobalClassBE.ButtonText.BtnCancel;
    }

    private void ShowTextForDelete()
    {
        lblDelete.Text = GlobalClassBE.LabelText.DeleteConfirm;

        btnYes.Text = GlobalClassBE.ButtonText.BtnYes;
        btnNo.Text = GlobalClassBE.ButtonText.BtnNo;
    }

    #endregion show text

    #region load data

    private DataTable GetDataSource(string id)
    {
        DataTable dt = null;
        try
        {
            GssBL.BLAdvImage BL = new GssBL.BLAdvImage();
            dt = BL.getAdvImageByAdvertise(id);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            dt = null;
        }
        return dt;
    }

    #endregion load data

    #region SaveState
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Page.RegisterRequiresControlState(this);
    }
    protected override object SaveControlState()
    {
        object[] state = new object[2];
        object baseState = base.SaveControlState();
        state[0] = baseState;
        state[1] = SelectedItemKeys;
        return state;
    }
    protected override void LoadControlState(object savedState)
    {
        if (savedState == null) return;
        if (savedState is object[])
        {
            object[] state = (object[])savedState;
            base.LoadControlState(state[0]);
            SelectedItemKeys = state[1];
        }
    }
    #endregion SaveState

    #region properties

    protected object m_ItemKeys;
    public virtual object SelectedItemKeys
    {
        get { return m_ItemKeys; }
        set { m_ItemKeys = value; }
    }
    #endregion properties

    /*    list    */

    protected void SelectedIndexChanged(int k)
    {
        this.grdData.SelectedIndex = k;
        m_ItemKeys = this.grdData.DataKeys[k].Value;
    }
    protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataControlRowType itemType = e.Row.RowType;
        if (itemType == DataControlRowType.DataRow)
        {
            string s = ((BoundField)this.grdData.Columns[0]).DataField.ToString();
            if (DataBinder.Eval(e.Row.DataItem, s).ToString().CompareTo("&nbsp;") != 0)
            {
                // add event to rows item on grid
                try
                {
                    e.Row.Attributes.Add("onmouseover", "changeColorStyle(this);");
                    e.Row.Attributes.Add("onmouseout", "resetColorStyle(this);");
                }
                catch (Exception exc)
                {
                    string mess = exc.Message.ToString();
                }
            }

            string image = ((DataRowView)e.Row.DataItem).Row["Image"].ToString();
            string ext = GlobalClassBE.getFileExtendtion(image);

            if (ext.Equals(".gif") || ext.Equals(".png") || ext.Equals(".jpg"))
                e.Row.Cells[3].Text = "<img src='../Photos/Advertise/" + image + "' width='50px' height='50px' />";
        }
    }
    protected void grdData_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string adv = "";
        if (Request.Params["adv"] != null)
            adv = Request.Params["adv"].ToString();

        int k = e.RowIndex;
        SelectedIndexChanged(k);
        string keys = (string)this.SelectedItemKeys;
        string sLink = String.Format(GlobalClassBE.URLFormat, GlobalClassBE.PageName.AdvImage, "delete") + "&id=" + keys + "&adv=" + adv;
        Response.Redirect(sLink, false);
    }
    protected void grdData_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string adv = "";
        if (Request.Params["adv"] != null)
            adv = Request.Params["adv"].ToString();

        int k = e.NewEditIndex;
        SelectedIndexChanged(k);
        string keys = (string)this.SelectedItemKeys;
        string sLink = String.Format(GlobalClassBE.URLFormat, GlobalClassBE.PageName.AdvImage, "edit") + "&id=" + keys + "&adv=" + adv;
        Response.Redirect(sLink, false);
    }
    protected void lkbAddNew_Click(object sender, EventArgs e)
    {
        string adv = "";
        if (Request.Params["adv"] != null)
            adv = Request.Params["adv"].ToString();

        string sLink = String.Format(GlobalClassBE.URLFormat, GlobalClassBE.PageName.AdvImage, "add") + "&adv=" + adv;
        Response.Redirect(sLink, false);
    }

    #region ShowAll

    private void ShowToAdd()
    {
        txtTitle.Text = "";
        txtURL.Text = "";
        txtWidth.Text = "0";
        txtHeight.Text = "0";
        txtLevel.Text = "0";

        ckbDeleteImages.Visible = false;
        lblDeleteImages.Visible = false;

        lblActionName.Text = GlobalClassBE.ActionText.Add;
        ShowButton(true);
        GlobalClassBE.MyMessage.MgsCurrentMessages = "";
    }

    private void ShowToEdit(string id)
    {
        try
        {
            GssDO.DOAdvImage DO = getByID(id);

            if (DO != null)
            {
                txtTitle.Text = DO.Title;
                txtURL.Text = DO.URL;
                txtWidth.Text = DO.Width.ToString();
                txtLevel.Text = DO.Level.ToString();
                txtHeight.Text = DO.Height.ToString();

                lblTempID.Text = DO.PKIDAdvImage;
                lblCurrentImages.Text = DO.Image;
                if (DO.Image.Trim() != "")
                {
                    ckbDeleteImages.Visible = true;
                    lblDeleteImages.Visible = true;
                }
                else
                {
                    ckbDeleteImages.Visible = false;
                    lblDeleteImages.Visible = false;
                }
                lblActionName.Text = GlobalClassBE.ActionText.EditPage;
                ShowButton(false);
                GlobalClassBE.MyMessage.MgsCurrentMessages = "";
            }
            else
            {
                GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Selected;
                CancelPage();
            }
        }
        catch (Exception exc)
        {
            string mess = exc.Message;
        }
    }

    private GssDO.DOAdvImage getByID(string id)
    {
        GssDO.DOAdvImage DO = null;
        try
        {
            GssBL.BLAdvImage BL = new GssBL.BLAdvImage();
            DO = BL.getAdvImageByID(id);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            DO = null;
        }
        return DO;
    }

    private GssDO.DOAdvertise getAdvertiseByID(string id)
    {
        GssDO.DOAdvertise DO = null;
        try
        {
            GssBL.BLAdvertise BL = new GssBL.BLAdvertise();
            DO = BL.getAdvertiseByID(id);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            DO = null;
        }
        return DO;
    }

    /// <summary>
    /// show button on form (Parameter: true: show to add, false: show to edit)
    /// </summary>
    /// <param name="pFlag">type bool: true: show to add; false: show to edit</param>
    private void ShowButton(bool pFlag)
    {
        btnSave.Visible = true;
        btnClear.Visible = true;
        btnCancel.Visible = true;
    }
    private void CancelPage()
    {
        string adv = "";
        if (Request.Params["adv"] != null)
            adv = Request.Params["adv"].ToString();

        Response.Redirect(GlobalClassBE.PageName.AdvImage + "?adv=" + adv, false);
    }
    private void ResetPage()
    {
        string adv = "";
        if (Request.Params["adv"] != null)
            adv = Request.Params["adv"].ToString();
        string actName = "";
        if (Request.Params[GlobalClassBE.MyUrlParameter.ActionName] != null)
            actName = Request.Params[GlobalClassBE.MyUrlParameter.ActionName].ToString();
        string sID = "";
        if (Request.Params["id"] != null)
            sID = Request.Params["id"].ToString();

        if (actName != "" && sID != "")
            Response.Redirect(String.Format(GlobalClassBE.URLFormat, GlobalClassBE.PageName.AdvImage, "edit") + "&adv=" + adv + "&id=" + sID, false);
        else
            Response.Redirect(String.Format(GlobalClassBE.URLFormat, GlobalClassBE.PageName.AdvImage, "add") + "&adv=" + adv, false);
    }
    private void SavePage(bool pFlag)
    {
        lblMessage.Text = "";
        if (validateForm())
        {
            string actName = "";
            if (Request.Params[GlobalClassBE.MyUrlParameter.ActionName] != null)
                actName = Request.Params[GlobalClassBE.MyUrlParameter.ActionName].ToString();
            if (actName == "add")
                SaveAdd(pFlag);
            else if (actName == "edit")
            {
                string sID = lblTempID.Text.ToString();
                if (sID != "")
                    SaveEdit(sID);
                else
                {
                    GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Selected;
                    CancelPage();
                }
            }
            else
            {
                CancelPage();
            }
        }
    }

    private void SaveAdd(bool pFlag)
    {
        try
        {
            GssDO.DOAdvImage DO = fillDO();
            GssBL.BLAdvImage BL = new GssBL.BLAdvImage();
            bool isUpload = true;
            if (fulImages.FileName.Trim() != "")
            {
                try
                {
                    // create root folder
                    System.IO.Directory.CreateDirectory(m_FolderImage);
                    // upload image to server
                    fulImages.PostedFile.SaveAs(m_FolderImage + DO.Image);
                    // 
                    isUpload = true;
                }
                catch (System.IO.IOException io)
                {
                    lblMessage.Text = io.Message;
                    lblMessage.Text = GlobalClassBE.MyMessage.MgsUpload_Fail + GlobalClassBE.getFileName(fulImages.FileName);
                    isUpload = false;
                }
                catch (Exception e)
                {
                    lblMessage.Text = e.Message;
                    lblMessage.Text = GlobalClassBE.MyMessage.MgsUpload_Fail + GlobalClassBE.getFileName(fulImages.FileName);
                    isUpload = false;
                }
            }
            if (isUpload)
            {
                BL.Insert(DO);
                PublishFashionAdvertise();
                GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsAdd_Success;
                if (!pFlag)
                    CancelPage();
                else
                    ResetPage();
            }
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            lblMessage.Text = GlobalClassBE.MyMessage.MgsAdd_UnSuccess;
        }
    }

    private void SaveEdit(string pID)
    {
        try
        {
            GssDO.DOAdvImage DO = fillDO(pID);
            GssBL.BLAdvImage BL = new GssBL.BLAdvImage();
            bool isUpload = true;
            if (ckbDeleteImages.Checked)
            {
                try
                {
                    System.IO.File.Delete(m_FolderImage + @"\" + lblCurrentImages.Text);
                    DO.Image = "";
                    isUpload = true;
                }
                catch (System.IO.IOException io)
                {
                    lblMessage.Text = io.Message;
                    lblMessage.Text = GlobalClassBE.MyMessage.MgsAccess_Deny;
                    isUpload = false;
                }
                catch (Exception e)
                {
                    lblMessage.Text = e.Message;
                    lblMessage.Text = GlobalClassBE.MyMessage.MgsAccess_Deny;
                    isUpload = false;
                }
            }
            else
            {
                if (fulImages.FileName.Trim() != "")
                {
                    try
                    {
                        // create root folder
                        System.IO.Directory.CreateDirectory(m_FolderImage);
                        // upload image to server
                        fulImages.PostedFile.SaveAs(m_FolderImage + DO.Image);
                        // 
                        isUpload = true;
                    }
                    catch (System.IO.IOException io)
                    {
                        lblMessage.Text = io.Message;
                        lblMessage.Text = GlobalClassBE.MyMessage.MgsUpload_Fail + GlobalClassBE.getFileName(fulImages.FileName);
                        isUpload = false;
                    }
                    catch (Exception e)
                    {
                        lblMessage.Text = e.Message;
                        lblMessage.Text = GlobalClassBE.MyMessage.MgsUpload_Fail + GlobalClassBE.getFileName(fulImages.FileName);
                        isUpload = false;
                    }
                }
            }
            if (isUpload)
            {
                BL.Update(DO);
                PublishFashionAdvertise();
                GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Success;
                CancelPage();
            }
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            lblMessage.Text = GlobalClassBE.MyMessage.MgsEdit_UnSuccess;
        }
    }

    private bool validateByLabel()
    {
        bool result = true;
        try
        {
            string actName = "";
            if (Request.Params[GlobalClassBE.MyUrlParameter.ActionName] != null)
                actName = Request.Params[GlobalClassBE.MyUrlParameter.ActionName].ToString();
            if (actName == "add" && fulImages.HasFile == false)
            {
                reqImage.Visible = true;
                result = false;
            }
            else
                reqImage.Visible = false;
            if (!GlobalClassBE.ValidateFileExtention(fulImages, GlobalClassBE.AdvertiseFile))
            {
                reqFormatImage.Visible = true;
                result = false;
            }
            else
                reqFormatImage.Visible = false;
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return result;
    }
    private bool validateForm()
    {
        bool result = true;
        try
        {
            if (!reqTitle.IsValid)
                result = false;
            if (!reqWidth.IsValid)
                result = false;
            if (!reqFormatNumberWidth.IsValid)
                result = false;
            if (!reqHeight.IsValid)
                result = false;
            if (!reqFormatNumberHeight.IsValid)
                result = false;
            if (!reqLevel.IsValid)
                result = false;
            if (!reqFormatNumberLevel.IsValid)
                result = false;
            if (result)
            {
                result = validateByLabel();
            }
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return result;
    }

    private GssDO.DOAdvImage fillDO()
    {
        string sID = GlobalClassBE.CreateGUI();
        GssDO.DOAdvImage DO = fillDO(sID);
        return DO;
    }

    private GssDO.DOAdvImage fillDO(string id)
    {
        GssDO.DOAdvImage DO = getByID(id);
        if (DO == null)
        {
            DO = new GssDO.DOAdvImage();
            DO.Tile_EN = "";
            DO.LogonID = "0";
            DO.CreatedDate = DateTime.Now;
            DO.StartedDate = DateTime.Now;
            DO.EndedDate = DateTime.Now;
            DO.IsDeleted = 0;
        }
        try
        {
            DO.PKIDAdvImage = id;
            DO.AdvertiseID = Request.Params["adv"].ToString();
            DO.Title = txtTitle.Text.Trim();
            if (!ckbDeleteImages.Checked)
            {
                if (fulImages.FileName.Trim() != "")
                    DO.Image = id + GlobalClassBE.getFileExtendtion(fulImages.FileName.Trim());
                else
                    DO.Image = lblCurrentImages.Text;
            }
            else
                DO.Image = "";
            DO.Level = 0;
            DO.URL = "";
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return DO;
    }
    #endregion ShowAll

    /***** Add Edit ******/
    protected void btnSave_Click(object sender, EventArgs e)
    {
        SavePage(false);
    }
    protected void btnContinuous_Click(object sender, EventArgs e)
    {
        SavePage(true);
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        ResetPage();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        CancelPage();
    }

    #region delete

    private void ShowToDelete(string id)
    {
        ShowTextForDelete();

        string deleteMessage = GlobalClassBE.MyMessage.DeletedAdvertiseConfirmMessage;

        GssDO.DOAdvImage DO = getByID(id);

        if (DO != null)
        {
            lblDeletedMessage.Text = String.Format(deleteMessage, DO.Title);
        }
        else
            CancelPage();
    }

    private void SaveDelete()
    {
        try
        {
            string id = "";
            if (Request.Params[GlobalClassBE.MyUrlParameter.ObjectIDName] != null)
                id = Request.Params[GlobalClassBE.MyUrlParameter.ObjectIDName].ToString();

            if (id != "")
            {
                GssDO.DOAdvImage DO = fillDODelete(id);
                GssBL.BLAdvImage BL = new GssBL.BLAdvImage();
                BL.Update(DO);
                PublishFashionAdvertise();
                GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsDelete_Success;
                CancelPage();
            }
            else
                CancelPage();
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            lblMessage.Text = GlobalClassBE.MyMessage.MgsDelete_UnSuccess;
        }
    }

    private GssDO.DOAdvImage fillDODelete(string id)
    {
        GssDO.DOAdvImage DO = null;
        try
        {
            DO = getByID(id);
            DO.IsDeleted = 1;
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return DO;
    }
    #endregion delete
    protected void btnYes_Click(object sender, EventArgs e)
    {
        SaveDelete();
    }
    protected void btnNo_Click(object sender, EventArgs e)
    {
        CancelPage();
    }

    private void PublishFashionAdvertise()
    {
        System.IO.StreamWriter sw = null;
        string filePath = AppDomain.CurrentDomain.BaseDirectory + @"App_Data\fashionadv.txt";

        DataTable dt = GetAdvertiseByPosition(1);
        if (dt != null && dt.Rows.Count > 0)
        {
            bool hasAdv = false;
            string objId = "fashionAdv", idControl = "idControl", idContainer = "idContainer", idSlide = "idSlide";
            string id = "", url = "", title = "", image = "";
            string rs = "";

            rs += "\n<marquee trueSpeed scrollAmount=\"1\" scrollDelay=\"55\" direction=\"up\" loop=\"infinite\"";
            rs += " style=\"padding: 0px; text-algin: left\" onmouseover=\"this.stop()\" onmouseout=\"this.start()\" width=\"179px\" height=\"140px\">";

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                id = dt.Rows[i]["IDAdvertise"].ToString();
                DataTable dtAdv = GetDataSource(id);
                if (dtAdv != null && dtAdv.Rows.Count > 0)
                {
                    hasAdv = true;
                    url = dtAdv.Rows[0]["URL"].ToString();
                    title = dtAdv.Rows[0]["Title"].ToString();
                    image = dtAdv.Rows[0]["Image"].ToString();
                    rs += "\n<div class='rows' id='" + idSlide + i.ToString() + "'>";
                    if (url != "")
                    {
                        rs += "<a href='" + image + "' target='_blank'>";
                        rs += "<img src='../Photos/Advertise/" + image + "' border='0' width='179' height='68' />";
                        rs += "</a>";
                    }
                    else
                    {
                        rs += "<img src='../Photos/Advertise/" + image + "' border='0' width='179' height='68' />";
                    }
                    rs += "\n</div>";
                }
            }
            rs += "\n</marquee>";

            // write empty advertise file
            sw = new System.IO.StreamWriter(filePath, false);// the file is overwritten
            if (hasAdv)
            sw.Write(rs);
            else
                sw.WriteLine("");
            sw.Close();
            sw.Dispose();
        }
        else
        {
            // write empty advertise file
            sw = new System.IO.StreamWriter(filePath, false);// the file is overwritten
            sw.WriteLine("");
            sw.Close();
            sw.Dispose();
        }
    }

    private DataTable GetAdvertiseByPosition(short position)
    {
        DataTable dt = null;
        try
        {
            GssBL.BLAdvertise BL = new GssBL.BLAdvertise();
            dt = BL.getAdvertiseByPosition(position);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            dt = null;
        }
        return dt;
    }
    /*
    private void PublishFashionAdvertise()
    {
        System.IO.StreamWriter sw = null;
        string filePath = AppDomain.CurrentDomain.BaseDirectory + @"App_Data\fashionadv.txt";

        DataTable dt = GetAdvertiseByPosition(1);
        if (dt != null && dt.Rows.Count > 0)
        {
            bool hasAdv = false;
            string objId = "fashionAdv", idControl = "idControl", idContainer = "idContainer", idSlide = "idSlide";
            string id = "", url = "", title = "", image = "";
            string rs = "";

            rs += "<div>";
            rs += "\n<div id=" + idControl;
            rs += "     onmouseover=\"ie_MsOver(this, " + objId + ");\"";
            rs += "     style=\"overflow: hidden; width: 198px; height: 75px\"";
            rs += "     onmouseout=\"ie_MsOut(this, " + objId + ");\">";
            rs += "\n<div id=" + idContainer + " style=\"visibility: hidden; position: relative\">";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                id = dt.Rows[i]["IDAdvertise"].ToString();
                DataTable dtAdv = GetDataSource(id);
                if (dtAdv != null && dtAdv.Rows.Count > 0)
                {
                    hasAdv = true;
                    url = dtAdv.Rows[0]["URL"].ToString();
                    title = dtAdv.Rows[0]["Title"].ToString();
                    image = dtAdv.Rows[0]["Image"].ToString();
                    rs += "\n<div class='rows' id='" + idSlide + i.ToString() + "'>";
                    if (url != "")
                    {
                        rs += "<a href='" + image + "' target='_blank'>";
                        rs += "<img src='../Photos/Advertise/" + image + "' border='0' width='179' height='68' />";
                        rs += "</a>";
                    }
                    else
                    {
                        rs += "<img src='../Photos/Advertise/" + image + "' border='0' width='179' height='68' />";
                    }
                    rs += "\n</div>";
                }
            }
            rs += "\n <script language=\"javascript\">";
            rs += "\n // Initialize rotator instance ";
            rs += "\n var " + objId + " = new ComponentArt_Rotator();";
            rs += "\n " + objId + ".GlobalID = '" + objId + "';";
            rs += "\n " + objId + ".ElementID = '" + idControl + "';";
            rs += "\n " + objId + ".ContainerID = '" + idContainer + "';";
            rs += "\n " + objId + ".ContainerRowID = 'idContainerRow';";
            rs += "\n " + objId + ".AutoStart = true;";
            rs += "\n " + objId + ".SlidePause = 10;";
            rs += "\n " + objId + ".HideEffect = null;";
            rs += "\n " + objId + ".HideEffectDuration = 521;";
            rs += "\n " + objId + ".Loop = true;";
            rs += "\n " + objId + ".PauseOnMouseOver = true;";
            rs += "\n " + objId + ".RotationType = 'ContentScroll';";
            rs += "\n " + objId + ".ScrollDirection = 'up';";
            rs += "\n " + objId + ".ScrollInterval = 20;";
            rs += "\n " + objId + ".ShowEffect = null;";
            rs += "\n " + objId + ".ShowEffectDuration = 521;";
            rs += "\n " + objId + ".SmoothScrollSpeed = 'Medium';";
            rs += "\n " + objId + ".Slides = new Array();";
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                id = dt.Rows[j]["IDAdvertise"].ToString();
                DataTable dtAdv = GetDataSource(id);
                if (dtAdv != null && dtAdv.Rows.Count > 0)
                {
                    rs += "\n " + objId + ".Slides[" + j.ToString() + "] = '" + idSlide + j.ToString() + "';";
                }
                
            }
            rs += "\n " + objId + ".HasTickers = false;";
            rs += "\n if (" + objId + ".AutoStart) rcr_Start(" + objId + ");";
            rs += "\n </script>";

            rs += "\n</div>";
            rs += "\n</div>";
            rs += "\n</div>";

            // write empty advertise file
            sw = new System.IO.StreamWriter(filePath, false);// the file is overwritten
            if (hasAdv)
            sw.Write(rs);
            else
                sw.WriteLine("");
            sw.Close();
            sw.Dispose();
        }
        else
        {
            // write empty advertise file
            sw = new System.IO.StreamWriter(filePath, false);// the file is overwritten
            sw.WriteLine("");
            sw.Close();
            sw.Dispose();
        }
    }
    private string ShowGeomancy(string lang, int number)
    {
        string rs = "";
        DataTable dt = GetGeomancyList(number);
        GssDO.DOGeomancy DO = new GssDO.DOGeomancy();
        if (dt != null && dt.Rows.Count > 0)
        {
            string objId = "oGeomancy", idControl = "idControl", idContainer = "idContainer", idSlide = "idSlide";
            rs += "<div>";
            rs += "<div id=" + idControl;
            rs += "     onmouseover=\"ie_MsOver(this, " + objId + ");\"";
            rs += "     style=\"OVERFLOW: hidden; WIDTH: 198px; HEIGHT: 75px\"";
            rs += "     onmouseout=\"ie_MsOut(this, " + objId + ");\">";
            rs += "<div id=" + idContainer + " style=\"VISIBILITY: hidden; POSITION: relative\">";

            for (int i = 0; i < dt.Rows.Count; i++ )
            {
                rs += "<div class='rows' id=" + idSlide + i.ToString() + ">";
                rs += "<a href='GeomancyDetail.aspx?id=" + dt.Rows[i][DO.PKIDGeomancyColumnName].ToString() + "'>" + dt.Rows[i][DO.TitleColumnName + lang].ToString() + "</a>";
                rs += "</div>";
            }

            rs += "\n <script language=\"javascript\">";
            rs += "\n // Initialize rotator instance -------------------------------------------------\n";
            rs += "\n var " + objId + " = new ComponentArt_Rotator();";
            rs += "\n " + objId + ".GlobalID = '" + objId + "';";
            rs += "\n " + objId + ".ElementID = '" + idControl + "';";
            rs += "\n " + objId + ".ContainerID = '" + idContainer + "';";
            rs += "\n " + objId + ".ContainerRowID = 'idContainerRow';";
            rs += "\n " + objId + ".AutoStart = true;";
            rs += "\n " + objId + ".SlidePause = 10;";
            rs += "\n " + objId + ".HideEffect = null;";
            rs += "\n " + objId + ".HideEffectDuration = 521;";
            rs += "\n " + objId + ".Loop = true;";
            rs += "\n " + objId + ".PauseOnMouseOver = true;";
            rs += "\n " + objId + ".RotationType = 'ContentScroll';";
            rs += "\n " + objId + ".ScrollDirection = 'up';";
            rs += "\n " + objId + ".ScrollInterval = 20;";
            rs += "\n " + objId + ".ShowEffect = null;";
            rs += "\n " + objId + ".ShowEffectDuration = 521;";
            rs += "\n " + objId + ".SmoothScrollSpeed = 'Medium';";
            rs += "\n " + objId + ".Slides = new Array();";
            for (int j = 0; j < dt.Rows.Count; j++ )
            {
                rs += "\n " + objId + ".Slides[" + j.ToString() + "] = '" + idSlide + j.ToString() + "';";
            }
            rs += "\n " + objId + ".HasTickers = false;";
            rs += "\n if (" + objId + ".AutoStart) rcr_Start(" + objId + ");";
            rs += "\n </script>";

            rs += "</div>";
            rs += "</div>";
            rs += "</div>";

        }
        return rs;
    }
    */
}
